/**
 * Created by hjqi on 2016/2/7.
 */

var OAuth2 = require('oauth').OAuth2;
var crypto = require('crypto');
module.exports = OAuth2Base;

function OAuth2Base(){
    //it should init in subclass
    this._opt = this._opt || {};

    this._accessTokenParam = this._accessTokenParam || {'redirect_uri':this._opt.callback_url};

    this._oauth2 = this.initOAuth2();

}

OAuth2Base.prototype.initOAuth2 = function(){
    return new OAuth2(
        this._opt.client_id,
        this._opt.secret,
        this._opt.baseurl,
        this._opt.authorize_url,
        this._opt.token_url,
        null);
}

OAuth2Base.prototype.getReqAuthUrlAndSt = function(){
    var _st = crypto.randomBytes(32).toString('hex');
    var _url = this._oauth2.getAuthorizeUrl({
        redirect_uri:this._opt.callback_url,
        scope: ['repo', 'user'],
        state:_st
    });
    return {
        st:_st,
        url:_url
    }
}
